<import file="block/common.html" name="url" :tpl_cfg="['urlName'=>'专题管理']" />
<div class="layui-fluid">
	<div class="layui-row layui-col-space15">
		<div class="layui-col-md12">
			<script type="text/html" id="toolbarAE">
				<div class="layui-form">
				<?php echo SysRolCk("contentspecial_add")?'<button class="layui-btn layui-btn-sm  data-add-btn"><i class="layui-icon">&#xe61f;</i>'.lang('add').'专题</button>':''; ?> 
				<div class="layui-inline layui-show-xs-block" style="color: aliceblue; width: 40px;height: 35px;"> | </div>
				<div class="layui-inline layui-show-xs-block">
					<input type="text" name="name" id="name" placeholder="按专题名搜索" class="layui-input">
				</div>
				<div class="layui-inline layui-show-xs-block">
					<button class="layui-btn layui-btn-sm " data-type="reload" id="searchBtn"><i class="layui-icon">&#xe615;</i></button>
				</div>
				</div>
			</script>
			<table class="layui-hide" id="currentTableId" lay-data="{id: 'idReload'}" lay-filter="currentTableFilter">
			</table>
			<script type="text/html" id="icoTableBar">
         	    <i class="layui-icon {{d.ico}}"></i> 
			</script>
			<script type="text/html" id="areaTableBar">
				{{# if(d.area == 0){return '不显示' }
					if(d.area == 1){return '头部'}
					if(d.area == 2){return '中部'}
					if(d.area == 3){return '下部'}
				}}
		   </script>
			<script type="text/html" id="switchTpl">
				<input type="checkbox" name="status" id="{{d.id}}" value="{{d.status}}" lay-skin="switch" lay-text="显|隐"
					lay-filter="switchStatus" {{ d.status == 1 ? 'checked' : '' }}>
			</script>

			<script type="text/html" id="currentTableBar">
				<div class="layui-btn-group">
			<?php echo SysRolCk("contentspecial_edit")?' <a class="layui-btn layui-btn-sm data-count-edit" lay-event="edit"><i class="layui-icon">&#xe642;</i>'.lang('edit').'</a>':''; ?> 
			<?php echo SysRolCk("contentspecial_del")?'<a class="layui-btn layui-btn-sm layui-btn-danger data-count-delete" lay-event="delete"><i class="layui-icon">&#xe640;</i>'.lang('del').'</a>':''; ?> 
				</div>
		</script>
		</div>
	</div>
</div>

<script>
	layui.define(function () {
		var table = layui.table,
			$ = layui.jquery,
			form = layui.form;

		form.render();

		table.render({
			elem: '#currentTableId',
			//url: '/res/admin/api/table.json',
			url: '<{CAUrl("contentspecial","index")}>&list_json',
			toolbar: '#toolbarAE',
			defaultToolbar: ['filter', 'exports', 'print', {
				title: '提示',
				layEvent: 'LAYTABLE_TIPS',
				icon: 'layui-icon-tips'
			}],
			page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
				layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']//自定义分页布局
				//,curr: 5 //设定初始在第 5 页
				, groups: 1 //只显示 1 个连续页码
				, first: false //不显示首页
				, last: false //不显示尾页
			},
			cols: [[
				//{type: "checkbox", width: 50, fixed: "left"},
				{ field: 'id', width: 80, title: 'ID', sort: true },
				{ field: 'name', title: '专题名称', width: 360 },
				{ field: 'url', title: '链接' },
				{ field: 'ico', title: '图标', width: 160, minWidth: 50, templet: '#icoTableBar', fixed: "right", align: "center" },
				{ field: 'sort', width: 80, title: '排序', sort: true, edit: 'text' },
				{ field: 'area', title: '首页显示', templet: '#areaTableBar' },
				{ field: 'status', width: 260, title: '显示状态', templet: '#switchTpl', unresize: true },
				{ title: '操作', width: 180, templet: '#currentTableBar', fixed: "right", align: "center" }
			]],
			limits: [10, 15, 20, 25, 50, 100],
			limit: 15,
			page: true,
			id: 'tableReload',
		});


		// 监听添加操作
		$(".data-add-btn").on("click", function () {
			loadMain('<{CAUrl("contentspecial","add")}>&view');
			return false;
		});

		//监听单元格编辑
		table.on('edit(currentTableFilter)', function (obj) {
			var value = obj.value //得到修改后的值
				, data = obj.data //得到所在行所有键值
				, field = obj.field; //得到字段
			$.post('<{CAUrl("contentspecial","edit")}>&sortedit&id=' + data.id, { sort: value }, function (res) {
				if (res.status == 1) {
					layer.msg(res.info, { icon: 1, time: 1000 }, function () {
						table.reloadData('tableReload');
					});
				} else {
					layer.msg(res.info, { icon: 2, anim: 6, time: 5000 });
				}
			});
		});

		//监听状态操作
		form.on('switch(switchStatus)', function (obj) {
			$.post('<{CAUrl("contentspecial","edit")}>&switchStatus',
				{ id: this.id, status: obj.elem.checked ? 1 : 0 },
				function (res) {
					layer.tips(res.info, obj.othis, { tips: 2 });
				});
		});

		//搜索重载表格
		$('#searchBtn').on('click', function () {
			table.reloadData('tableReload', {
				method: 'get',
				where: {
					name: $('#name').val(),
				},
				page: {
					curr: 1
				},
				page: true,
			});
			return false;
		});


		table.on('tool(currentTableFilter)', function (obj) {
			var data = obj.data;
			if (obj.event === 'edit') {
				loadMain('<{CAUrl("contentspecial","edit")}>&view&id=' + data.id);
				return false;
			} else if (obj.event === 'delete') {
				layer.confirm('真的删除么', function (index) {
					$.post('<{CAUrl("contentspecial","del")}>', data, function (res) {
						if (res.status == 1) {
							layer.msg(res.info, { icon: 1, time: 1000 }, function () {
								obj.del();
								layer.close(index);
							});
						} else {
							layer.msg(res.info, { icon: 2, anim: 6, time: 5000 });
						}
					});
				});
			}
		});
	});
</script>